#include<iostream>
#include<algorithm>
using namespace std;

class Solution {
public:
    string reverseOnlyLetters(string s) {
        int begin = 0;
        int end = s.size() - 1;
        while (begin < end)
        {
            if ((s[begin] <= 'z' && s[begin] >= 'a') || (s[begin] >= 'A' && s[begin] <= 'Z'))
            {
                if ((s[end] <= 'z' && s[end] >= 'a') || (s[end] >= 'A' && s[end] <= 'Z'))
                {
                    swap(s[begin], s[end]);
                    begin++;
                    end--;
                }
                else end--;
            }
            else
            {
                begin++;
            }

        }
        return s;
    }
};